package com.jinzecloud.blog.api;

import com.jinzecloud.blog.bean.User;
import com.jinzecloud.blog.constant.UserEnum;
import com.jinzecloud.blog.service.UserService;
import com.jinzecloud.blog.utils.Result;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

//@Api(tags = "用户前端 API 接口")
@RestController
@RequestMapping("/api/user")
public class UserLoginController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
//    @ApiOperation(value = "用户登录", notes = "用户可以进行登录操作")
    public Result userLogin(@RequestBody User user){
        Map map = userService.login(user);
        if (!UserEnum.succeed.getStatus().equals(map.get("status"))){
            return Result.error(Integer.parseInt(map.get("status").toString()),map.get("msg").toString());
        }
        return Result.ok().put("data",map);

    }

    @RequestMapping("/register")
//    @ApiOperation(value = "用户登录", notes = "用户可以进行登录操作")
    public Result userregister(@RequestBody User user){
        if (checkName(user.getUUsername()))
        userService.add(user);
        return null;
    }

    private boolean checkName(String username) {
        return userService.checkName(username);
    }

}
